<template>
  <span>
    <el-dialog title="执行构建" :visible.sync="dialogFormVisible" :append-to-body="true">
      <span v-if="dialogFormVisible && formDataJson.length > 0" >
        <el-form ref="paramsForm">
          <el-form-item v-for="(item,index) in formDataJson"
                        :label="item.name"
                        :label-width="formLabelWidth"
                        :key="index">
            <el-tooltip :hide-after="0"
                        :content="item.description? JSON.parse(item.description): '没有描述参数含义'"
                        placement="top">
              <el-input v-if="item.type === 'input'" v-model="item.value"></el-input>
            </el-tooltip>
          </el-form-item>
      </el-form>
      </span>
      <span v-else>
        {{ '无构建参数，请点击确认执行构建' }}
      </span>
      <div slot="footer" class="dialog-footer">
        <el-button size="small"  @click="dialogFormVisible = false"> 取 消 </el-button>
        <el-button size="small"  type="primary" @click="handleOK"> 确 定 </el-button>
      </div>
    </el-dialog>
  </span>
</template>

<script>
import {buildJob, getJobParams} from "@/api/build";

export default {
  name: "BuildParams",
  data() {
    return {
      dialogFormVisible: false,
      formLabelWidth: '120px',
      formDataJson:[],
      jenkinsName: '',
      jobFullName: '',
      scriptId: ''
      // formDataJson: [
      //   {  //Json串
      //     type: 'input',
      //     name: 'name',
      //     value: 'defaultValue',
      //   }
      // ],
    }
  },
  methods: {
    showDialog: function (index=0, row={}) {
      this.dialogFormVisible = true
      this.jenkinsName = row.jenkinsName
      this. jobFullName = row.jobFullName
      this.scriptId = row.scriptId
      this.getBuildPrams(this.jenkinsName, this.jobFullName)
    },
    handleOK: function () {
      this.buildJobNow()
      this.dialogFormVisible = false
    },
    getBuildPrams: function (jenkinsName, jobFullName) {
      let params = {
        jenkinsName: jenkinsName,
        jobFullName: jobFullName
      }
      getJobParams(params).then(res => {
        if (res.code === 200 && res.data) {
            this.formDataJson = res.data.map(item => {
              item.type = 'input'
              item.name = JSON.parse(item.name)
              item.value = JSON.parse(item.value)
              return item
            })
          } else {
          this.formDataJson = []
        }
      })
    },
    buildJobNow: function () {
      let jobParamsObj = {}
      this.formDataJson.map(item => {
        jobParamsObj[item.name] = item.value
      })
      let params = {
        jenkinsName: this.jenkinsName,
        jobFullName: this.jobFullName,
        jobParams: JSON.stringify(jobParamsObj)
      }
      buildJob(params).then(res => {
        if (res.code === 200 && res.data) {
          this.$alert('发送执行构建请求成功，请稍后查看执行结果', '脚本构建', {
            confirmButtonText: '确定',
            callback: action => {
              this.$message({
                type: 'success',
                message: `action: ${ action }`
              });
            }
          });
        }
        this.$emit('fetchScriptData')
        this.$emit('getBuildList', Number(this.scriptId))
      })
    }
  }
}
</script>

<style scoped>

</style>
